package com.hiultra.archive.service.impl;

import java.awt.event.FocusEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hiultra.archive.controller.ArchiveBoxIllegalDetailRecordsController;
import com.hiultra.archive.domain.ArcArchiveRecords;
import com.hiultra.archive.domain.ArchiveBoxIllegalDetailRecords;
import com.hiultra.archive.domain.vo.ArchiveBoxIllegalRecordsReportVo;
import com.hiultra.archive.domain.vo.ResultEntity;
import com.hiultra.archive.mapper.ArchiveBoxIllegalDetailRecordsMapper;
import com.hiultra.archive.utils.tool.ToolUtils;
import com.hiultra.common.constant.BusinessConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.hiultra.archive.mapper.ArchiveBoxIllegalRecordsMapper;
import com.hiultra.archive.domain.ArchiveBoxIllegalRecords;
import com.hiultra.archive.service.IArchiveBoxIllegalRecordsService;

/**
 * 非法携带盒记录Service业务层处理
 *
 * @author 派大星
 * @date 2024-07-23
 */
@Service
public class ArchiveBoxIllegalRecordsServiceImpl implements IArchiveBoxIllegalRecordsService {
    @Autowired
    private ArchiveBoxIllegalRecordsMapper archiveBoxIllegalRecordsMapper;
    @Autowired
    private ArchiveBoxIllegalDetailRecordsMapper archiveBoxIllegalDetailRecordsMapper;
    @Value("${pubKey}")
    private String pubKey;

    @Value("${priKey}")
    private String priKey;
    @Value("${urlIp}")
    private String urlIp;
    @Value("${archiveBoxIllegalRecordsReportUrl}")
    private String archiveBoxIllegalRecordsReportUrl;

    /**
     * 查询非法携带盒记录
     *
     * @param id 非法携带盒记录主键
     * @return 非法携带盒记录
     */
    @Override
    public ArchiveBoxIllegalRecords selectArchiveBoxIllegalRecordsById(Long id) {
        return archiveBoxIllegalRecordsMapper.selectArchiveBoxIllegalRecordsById(id);
    }

    /**
     * 查询非法携带盒记录列表
     *
     * @param archiveBoxIllegalRecords 非法携带盒记录
     * @return 非法携带盒记录
     */
    @Override
    public List<ArchiveBoxIllegalRecords> selectArchiveBoxIllegalRecordsList(ArchiveBoxIllegalRecords archiveBoxIllegalRecords) {
        return archiveBoxIllegalRecordsMapper.selectArchiveBoxIllegalRecordsList(archiveBoxIllegalRecords);
    }

    /**
     * 新增非法携带盒记录
     *
     * @param archiveBoxIllegalRecords 非法携带盒记录
     * @return 结果
     */
    @Override
    public int insertArchiveBoxIllegalRecords(ArchiveBoxIllegalRecords archiveBoxIllegalRecords) {
        return archiveBoxIllegalRecordsMapper.insertArchiveBoxIllegalRecords(archiveBoxIllegalRecords);
    }

    /**
     * 修改非法携带盒记录
     *
     * @param archiveBoxIllegalRecords 非法携带盒记录
     * @return 结果
     */
    @Override
    public int updateArchiveBoxIllegalRecords(ArchiveBoxIllegalRecords archiveBoxIllegalRecords) {
        return archiveBoxIllegalRecordsMapper.updateArchiveBoxIllegalRecords(archiveBoxIllegalRecords);
    }

    /**
     * 批量删除非法携带盒记录
     *
     * @param ids 需要删除的非法携带盒记录主键
     * @return 结果
     */
    @Override
    public int deleteArchiveBoxIllegalRecordsByIds(Long[] ids) {
        return archiveBoxIllegalRecordsMapper.deleteArchiveBoxIllegalRecordsByIds(ids);
    }

    /**
     * 删除非法携带盒记录信息
     *
     * @param id 非法携带盒记录主键
     * @return 结果
     */
    @Override
    public int deleteArchiveBoxIllegalRecordsById(Long id) {
        return archiveBoxIllegalRecordsMapper.deleteArchiveBoxIllegalRecordsById(id);
    }

    /**
     * 非法携带档案记录上报接口
     *
     * @param archiveBoxIllegalRecords 非法携带盒记录
     * @return 结果
     */
    @Override
    public int reportArchiveBoxIllegalRecords(ArchiveBoxIllegalRecords archiveBoxIllegalRecords) {
        ArchiveBoxIllegalRecordsReportVo archiveBoxIllegalRecordsReportVo = ToolUtils.convertEntity(archiveBoxIllegalRecords, ArchiveBoxIllegalRecordsReportVo::new);
        if (ObjectUtil.isNotNull(archiveBoxIllegalRecords.getAlarmTime())) {
            archiveBoxIllegalRecordsReportVo.setAlarmTime(DateUtil.format(archiveBoxIllegalRecords.getAlarmTime(), "yyyyMMddHHmmss"));
        }
        if (archiveBoxIllegalRecords.getBoxList().size() > 0) {
            List<ArchiveBoxIllegalRecordsReportVo.ArchiveBox> archiveBoxList = ToolUtils.copyList(archiveBoxIllegalRecords.getBoxList(), ArchiveBoxIllegalRecordsReportVo.ArchiveBox::new);
            archiveBoxIllegalRecordsReportVo.setBoxList(archiveBoxList);
        }
        String jsonString = "";
        // 将JsonData对象转换为JSON字符串
        try {
            jsonString = new ObjectMapper().writeValueAsString(archiveBoxIllegalRecordsReportVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(jsonString);

        //发送请求获取数据
        HashMap hashMap = ToolUtils.sendPostRequest(BusinessConstants.ARCHIVE_BOX_ILLEGAL_RECORDS_REPORT_NO, urlIp, archiveBoxIllegalRecordsReportUrl, "非法携带档案数据上传", jsonString, pubKey, priKey);

        System.out.println("非法携带档案记录上报接口Url" + archiveBoxIllegalRecordsReportUrl);
        System.out.println("非法携带档案记录上报接口" + hashMap.toString());
        if (hashMap.get("code").equals("00000")) {
            archiveBoxIllegalRecords.setPushStatus(BusinessConstants.END_PUSHSTATUS);
            System.out.println("非法携带档案记录上报接口成功,推送状态修改为已推送！");
        } else {
            System.out.println("非法携带档案记录上报接口失败");
        }

        return archiveBoxIllegalRecordsMapper.updateArchiveBoxIllegalRecords(archiveBoxIllegalRecords);

    }

    /**
     * 非法携带档案记录定时上报接口
     *
     * @return 结果
     */
    @Override
    public ResultEntity timingArchiveBoxIllegalRecordsResultReport() {
        List<ArchiveBoxIllegalRecords> archiveBoxIllegalRecordsList = archiveBoxIllegalRecordsMapper.selectArchiveBoxIllegalRecordsList(new ArchiveBoxIllegalRecords() {{
            setPushStatus(BusinessConstants.PUSHSTATUS);
        }});

        if (archiveBoxIllegalRecordsList.size() > 0) {
            for (ArchiveBoxIllegalRecords archiveBoxIllegalRecords : archiveBoxIllegalRecordsList) {
                List<ArchiveBoxIllegalDetailRecords> archiveBoxIllegalDetailRecordsList = archiveBoxIllegalDetailRecordsMapper.selectArchiveBoxIllegalDetailRecordsList(new ArchiveBoxIllegalDetailRecords() {{
                    setRecordId(archiveBoxIllegalRecords.getId());
                }});
                archiveBoxIllegalRecords.setBoxList(archiveBoxIllegalDetailRecordsList);
                reportArchiveBoxIllegalRecords(archiveBoxIllegalRecords);
            }
        }
        return ResultEntity.success();
    }
}
